package com.tian.controller;

import com.tian.pojo.User;
import com.tian.service.UserService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getAllUser")
    @ResponseBody
    public List<User> getAllUser(){
        return userService.getAllUser();
    }
    @RequestMapping("/updateInfo")
    public String updateInfo(@RequestPart("password") String password,
                             @RequestPart("face") MultipartFile face, HttpSession session){
        if(!face.isEmpty()){
            try {
                User user= (User) session.getAttribute("visitor");
                //获取文件后缀名
                String originalFilename=face.getOriginalFilename();
                String finalFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                face.transferTo(new File("E://view//image//"+finalFileName));
                String path="/image/"+finalFileName;
                userService.updateUser(user.getAccount(),password,path);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return "UserMain";
    }
    @RequestMapping("/toUpdate")
    public String toUpdate(){
        return "userupdate";
    }
}
